﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UygulamaTakbisService.wsTakbisServis;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Diagnostics;

namespace UygulamaTakbisService
{
    public class HissedarDao : IDisposable
    {
        TAKBISWebService takbisServis = new TAKBISWebService();

        public bool HissedarKaydet(Hissedar hissedar, TapuBolum tapuBolum, decimal referans)
        {
            bool sonuc;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    SqlTransaction transaction = con.BeginTransaction("trans");
                    try
                    {
                        cmd.CommandText = @"INSERT INTO Hissedar with(updlock) (HissedarID,Referans,TapuBolum,MalikTip,MalikID,MalikOzetBilgi,HisseTipi,IstirakNo,HissePay,HissePayda,TapuBolumDurum,TesisIslemBilgiID,TerkinIslemBilgiID,KayitTarih) VALUES (@HissedarID,@Referans,@TapuBolum,@MalikTip,@MalikID,@MalikOzetBilgi,@HisseTipi,@IstirakNo,@HissePay,@HissePayda,@TapuBolumDurum,@TesisIslemBilgiID,@TerkinIslemBilgiID,GETDATE())";

                        if (hissedar.HissedarID != null)
                            cmd.Parameters.Add("@HissedarID", hissedar.HissedarID);
                        else
                            cmd.Parameters.Add("@HissedarID", DBNull.Value);

                        cmd.Parameters.Add("@Referans", referans);
                        cmd.Parameters.Add("@TapuBolum", tapuBolum);

                        if (hissedar.Malik != null)
                            cmd.Parameters.Add("@MalikTip", (byte)hissedar.Malik.MalikTip);
                        else
                            cmd.Parameters.Add("@MalikTip", DBNull.Value);

                        if (hissedar.Malik.MalikID != null)
                            cmd.Parameters.Add("@MalikID", hissedar.Malik.MalikID);
                        else
                            cmd.Parameters.Add("@MalikID", DBNull.Value);

                        if (hissedar.Malik.MalikOzetBilgi != null)
                            cmd.Parameters.Add("@MalikOzetBilgi", hissedar.Malik.MalikOzetBilgi);
                        else
                            cmd.Parameters.Add("@MalikOzetBilgi", DBNull.Value);

                        if (hissedar.Hisse.HisseTipi != null)
                            cmd.Parameters.Add("@HisseTipi", (byte)hissedar.Hisse.HisseTipi);
                        else
                            cmd.Parameters.Add("@HisseTipi", DBNull.Value);

                        if (hissedar.Hisse.IstirakNo != null)
                            cmd.Parameters.Add("@IstirakNo", hissedar.Hisse.IstirakNo);
                        else
                            cmd.Parameters.Add("@IstirakNo", DBNull.Value);

                        if (hissedar.Hisse.HissePay != null)
                            cmd.Parameters.Add("@HissePay", hissedar.Hisse.HissePay);
                        else
                            cmd.Parameters.Add("@HissePay", DBNull.Value);

                        if (hissedar.Hisse.HissePayda != null)
                            cmd.Parameters.Add("@HissePayda", hissedar.Hisse.HissePayda);
                        else
                            cmd.Parameters.Add("@HissePayda", DBNull.Value);

                        if (hissedar.Hisse.TapuBolumDurum != null)
                            cmd.Parameters.Add("@TapuBolumDurum", (byte)hissedar.Hisse.TapuBolumDurum);
                        else
                            cmd.Parameters.Add("@TapuBolumDurum", DBNull.Value);

                        if (hissedar.Hisse.TesisIslem != null)
                            cmd.Parameters.Add("@TesisIslemBilgiID", hissedar.Hisse.TesisIslem.IslemBilgiID);
                        else
                            cmd.Parameters.Add("@TesisIslemBilgiID", DBNull.Value);

                        if (hissedar.Hisse.TerkinIslem != null)
                            cmd.Parameters.Add("@TerkinIslemBilgiID", hissedar.Hisse.TerkinIslem.IslemBilgiID);
                        else
                            cmd.Parameters.Add("@TerkinIslemBilgiID", DBNull.Value);

                        cmd.Connection = con;
                        cmd.Transaction = transaction;
                        cmd.ExecuteScalar();
                        transaction.Commit();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        sonuc = false;
                        using (ExceptionLogDao el = new ExceptionLogDao())
                        {
                            el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                        }
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }

                    if (sonuc)
                    {
                        //hissedar.Hisse TesisIslemBilgi Kayıt
                        if (hissedar.Hisse.TesisIslem != null)
                        {
                            using (IslemBilgiDao islemBilgiDao = new IslemBilgiDao())
                            {
                                if (!islemBilgiDao.IslemBilgiKayitliMi(hissedar.Hisse.TesisIslem))
                                {
                                    bool tesisIslemBilgiKayitEdildiMi = islemBilgiDao.IslemBilgiKaydet(hissedar.Hisse.TesisIslem);

                                    if (tesisIslemBilgiKayitEdildiMi = true && !islemBilgiDao.YevmiyeBilgiKayitliMi(hissedar.Hisse.TesisIslem.Yevmiye))
                                    {
                                        bool tesisYevmiyeBilgiKayitEdildiMi = islemBilgiDao.YevmiyeBilgiKaydet(hissedar.Hisse.TesisIslem.Yevmiye);
                                    }
                                }
                            }
                        }

                        //hissedar.Hisse TerkinIslemBilgi Kayıt
                        if (hissedar.Hisse.TerkinIslem != null)
                        {
                            using (IslemBilgiDao islemBilgiDao = new IslemBilgiDao())
                            {
                                if (!islemBilgiDao.IslemBilgiKayitliMi(hissedar.Hisse.TerkinIslem))
                                {
                                    bool terkinIslemBilgiKayitEdildiMi = islemBilgiDao.IslemBilgiKaydet(hissedar.Hisse.TerkinIslem);

                                    if (terkinIslemBilgiKayitEdildiMi = true && !islemBilgiDao.YevmiyeBilgiKayitliMi(hissedar.Hisse.TerkinIslem.Yevmiye))
                                    {
                                        bool terkinYevmiyeBilgiKayitEdildiMi = islemBilgiDao.YevmiyeBilgiKaydet(hissedar.Hisse.TerkinIslem.Yevmiye);
                                    }
                                }
                            }
                        }
                    }

                    return sonuc;
                }
            }
        }

        public bool HissedarKayitliMi(decimal hissedarID)
        {
            bool sonuc = false;
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
                {
                    string sql = string.Format(@"SELECT * FROM Hissedar with(nolock) WHERE HissedarID = {0} ", hissedarID);
                    using (SqlCommand cmd = new SqlCommand(sql))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            cmd.CommandTimeout = 60000;
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            if (dt.Rows.Count >= 1)
                                sonuc = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return sonuc;
        }

        public List<Hissedar> GetirHissedarTasinmazIDDen(decimal tasinmazID, TapuBolumDurum tapuBolumDurum, long kullaniciID)
        {
            List<Hissedar> tasinmazHissedarList = null;
            try
            {
                using (LogDao logDao = new LogDao())
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    tasinmazHissedarList = takbisServis.GetirHissedarTasinmazIDDen(tasinmazID, tapuBolumDurum).ToList();
                    stopwatch.Stop();

                    Stopwatch stopwatch2 = new Stopwatch();
                    stopwatch2.Start();
                    foreach (Hissedar hissedar in tasinmazHissedarList)
                    {
                        bool hissedarKayitEdildiMi = HissedarKaydet(hissedar, TapuBolum.Tasinmaz, tasinmazID);
                    }
                    stopwatch2.Stop();
                    logDao.TakbisBilgiSorguLogKaydet("GetirHissedarTasinmazIDDen", null, tasinmazHissedarList, "TaşınmazID = " + tasinmazID.ToString() + ", TapuBolumDurum = " + tapuBolumDurum.ToString(), Convert.ToDecimal(stopwatch.ElapsedMilliseconds), Convert.ToDecimal(stopwatch2.ElapsedMilliseconds), kullaniciID);
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return tasinmazHissedarList;
        }

        public void Dispose()
        {
        }
    }
}
